package echecs;

import iechecs.Chaine;
import iechecs.IConstantes;
import iechecs.type.Liste;
import iechecs.Nat;

public class Case implements IConstantes {

    public final static Case HorsEchquier;
    public final static Case VIDE;
    private Nat x,  y;
    private final static Liste<Case> LCASES;


    static {
        VIDE = new Case();
        HorsEchquier = new Case(__1, __1);
        LCASES = new Liste<Case>(144);
        int i = 0;
        for (int y = 0; y < 12; y++) {
            for (int x = 0; x < 12; x++) {
                LCASES.add(i++, HorsEchquier);
            }
        }
        for (int y = 2; y < 10; y++) {
            for (int x = 2; x < 10; x++) {
                LCASES.add(x + 10 * y, new Case(new Nat(x), new Nat(y)));
            }
        }
    }

    private Case() {
    }

    private Case(Nat x, Nat y) {
        this.x = x;
        this.y = y;
    }

    public static Case valueOf(Nat x, Nat y) {

        return LCASES.get(x.intValue() + 10 * (y.intValue()));

    }

    public static Case valueOf(Chaine Scase) {
        Nat cl = _2.add(colonnes.indexOf(Scase.charAt(_0)));
        Nat rg = _1.add(Nat.parseInt(Scase.charAt(_1) + ""));
        return valueOf(cl, rg);

    }

    public Case plus(Direction dir) {
        return LCASES.get(x.add(dir.getU()).intValue() + 10 * (y.add(dir.getV())).intValue());
    }

    public Chaine toChaine() {
        String chaine = colonnes.charAt(__2.add(x)) + Nat.toString(__1.add(y));
        return new Chaine(chaine);
    }

    @Override
    public String toString() {
        return toChaine().toString();
    }

    @Override
    public boolean equals(Object o) {
        super.equals(o);
        Case c = (Case) o;
        return (x == c.x) && (y == c.y);
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.x != null ? this.x.hashCode() : 0);
        hash = 97 * hash + (this.y != null ? this.y.hashCode() : 0);
        return hash;
    }

    public static Case getLCASES(int x, int y) {
        return LCASES.get(x + 10 * y);
    }

    public Nat getX() {
        return x;
    }

    public Nat getY() {
        return y;
    }
}
